from flask import current_app
from flask import jsonify
from flask import session

from info.models import House
from info.modules.detail import detail_blu
from info.response_code import RET


@detail_blu.route("/houses/<int:house_id>")
def get_house_detail(house_id):

    user_id = session.get("user_id")
    if not user_id:
        user_id = -1

    if not house_id:
        return jsonify(errno=RET.PARAMERR, errmsg="参数不存在")

    try:
        house = House.query.filter(House.id == house_id).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询数据失败")
    if not house:
        return jsonify(errno=RET.NODATA, errmsg="房屋不存在")

    data = {
        "house": house.to_full_dict(),
        'user_id': user_id
    }

    return jsonify(errno=RET.OK, errmsg="OK", data=data)


